package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.api.applytrip.metadata.EApplyTripTrafficType;
import com.corpgovernment.api.organization.model.post.PostVo;
import com.corpgovernment.api.travelstandard.enums.TravelStandardTypeEnum;
import com.corpgovernment.api.travelstandard.soa.TravelStandardPostClient;
import com.corpgovernment.api.travelstandard.vo.FlightTravelStandardSoaVo;
import com.corpgovernment.api.travelstandard.vo.HotelTravelStandardManageVo;
import com.corpgovernment.api.travelstandard.vo.MbTravelstandFlightVo;
import com.corpgovernment.api.travelstandard.vo.MbTravelstandHotelVo;
import com.corpgovernment.api.travelstandard.vo.MbTravelstandTrainVo;
import com.corpgovernment.api.travelstandard.vo.TrainTravelStandardSoaVo;
import com.corpgovernment.api.travelstandard.vo.TravelStandardPostVo;
import com.corpgovernment.api.travelstandard.vo.TravelStandardSoaRequestVo;
import com.corpgovernment.api.travelstandard.vo.intlflight.IntlFlightManageVo;
import com.corpgovernment.api.travelstandard.vo.intlflight.MbTravelstandIntlFlightVo;
import com.corpgovernment.api.travelstandard.vo.request.QueryProductSwitchRequest;
import com.corpgovernment.api.travelstandard.vo.response.QueryProductSwitchResponse;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.organization.service.IPostService;
import com.corpgovernment.travelstandard.entity.db.MbTravelStandardAirportTransfer;
import com.corpgovernment.travelstandard.entity.db.MbTravelStandardTrainTransfer;
import com.corpgovernment.travelstandard.entity.db.MbTravelstandReasonCode;
import com.corpgovernment.travelstandard.entity.db.TravelStandardPost;
import com.corpgovernment.travelstandard.loader.MbTravelStandardAirportTransferLoader;
import com.corpgovernment.travelstandard.loader.MbTravelStandardReasonCodeLoader;
import com.corpgovernment.travelstandard.loader.MbTravelStandardTrainTransferLoader;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/***
 * @author zwmao
 * @version 1.0.0
 * @date 2024/1/26
 **/
@Service
@Slf4j
public class TravelStandardManageService {
    @Autowired
    private TravelStandardPostClient travelStandardPostClient;
    @Autowired
    private IPostService postService;
    @Autowired
    private TravelStandardPostService standPostService;
    @Autowired
    private MbTravelstandFlightService travelstandFlightService;
    @Autowired
    private MbTravelstandHotelService travelstandHotelService;
    @Autowired
    private MbTravelstandIntlFlightService travelstandIntlFlightService;
    @Autowired
    private MbTravelstandCarService travelstandCarService;
    @Autowired
    private MbTravelstandTrainService travelstandTrainService;
    @Autowired
    private MbTravelStandardTrainTransferLoader trainTransferStandardService;
    @Autowired
    private MbTravelStandardAirportTransferLoader airportTransferStandardService;

    @Autowired
    @Qualifier(value = "productSwitchQueryThreadPoolExecutor")
    private ThreadPoolExecutor productSwitchQueryThreadPoolExecutor;

    public QueryProductSwitchResponse queryTravelStandardByProduct(QueryProductSwitchRequest request) {
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getQueryInfoList())){
            return null;
        }
        log.info("当前请求的request为:{}", JsonUtils.toJsonString(request));
        QueryProductSwitchResponse response = new QueryProductSwitchResponse();
        ArrayList<QueryProductSwitchResponse.ProductSwitchInfo> productSwitchInfoList = new ArrayList<>();
        response.setTrafficType(request.getTrafficType());
        EApplyTripTrafficType type = EApplyTripTrafficType.getByValue(request.getTrafficType());
        request.getQueryInfoList().forEach(e ->{
            productSwitchInfoList.add(productSwitchConvert(e, type));
        });
        response.setProductSwitchInfoList(productSwitchInfoList);
        log.info("查询到的产线管控开关信息为:{}", JsonUtils.toJsonString(response));
        return response;
    }

    private QueryProductSwitchResponse.ProductSwitchInfo productSwitchConvert(QueryProductSwitchRequest.QueryInfo e, EApplyTripTrafficType type) {
        QueryProductSwitchResponse.ProductSwitchInfo productSwitchInfo = new QueryProductSwitchResponse.ProductSwitchInfo();
        productSwitchInfo.setOrgId(e.getOrgId());
        productSwitchInfo.setEmployeeType(e.getEmployeeType());
        productSwitchInfo.setUid(e.getUid());
        // 产线类型
        TravelStandardTypeEnum standardTypeEnum = TravelStandardTypeEnum.getByTrafficId(type.getValue());
        if(standardTypeEnum==null){
            return productSwitchInfo;
        }
        // 差标id
        Long tsId = getTsIdByUidAndOrgId(e.getOrgId(),e.getUid(), standardTypeEnum.getCode());
        if(tsId==null){
            return productSwitchInfo;
        }

        switch (type){
            case FLIGHT:
                MbTravelstandFlightVo travelStandFlight = travelstandFlightService.get(tsId);
                productSwitchInfo.setProductSwitch(Optional.ofNullable(travelStandFlight).map(MbTravelstandFlightVo::getProductSwitch).orElse("E"));
                break;
            case FLIGHTINTL:
                MbTravelstandIntlFlightVo travelStandIntlFlight = travelstandIntlFlightService.get(tsId);
                productSwitchInfo.setProductSwitch(Optional.ofNullable(travelStandIntlFlight).map(MbTravelstandIntlFlightVo::getProductSwitch).orElse("E"));
                break;
            case HOTEL:
                MbTravelstandHotelVo travelStandHotel = travelstandHotelService.get(tsId);
                productSwitchInfo.setProductSwitch(Optional.ofNullable(travelStandHotel).map(MbTravelstandHotelVo::getHotelProductSwitch).orElse("E"));
                break;
            case HOTELINTL:
                MbTravelstandHotelVo travelStandIntlHotel = travelstandHotelService.get(tsId);
                productSwitchInfo.setProductSwitch(Optional.ofNullable(travelStandIntlHotel).map(MbTravelstandHotelVo::getHotelIntlProductSwitch).orElse("E"));
                break;
            case TRAIN:
                MbTravelstandTrainVo travelStandTrain = travelstandTrainService.get(tsId);
                productSwitchInfo.setProductSwitch(Optional.ofNullable(travelStandTrain).map(MbTravelstandTrainVo::getProductSwitch).orElse("E"));
                break;
            case TRAINTRANSFER:
                MbTravelStandardTrainTransfer travelStandTrainTransfer = trainTransferStandardService.selectById(tsId);
                productSwitchInfo.setProductSwitch(Optional.ofNullable(travelStandTrainTransfer).map(MbTravelStandardTrainTransfer::getProductSwitch).orElse("E"));
                break;
            case AIRPORTTRANSFER:
                MbTravelStandardAirportTransfer travelStandAirportTransfer = airportTransferStandardService.selectById(tsId);
                productSwitchInfo.setProductSwitch(Optional.ofNullable(travelStandAirportTransfer).map(MbTravelStandardAirportTransfer::getProductSwitch).orElse("E"));
                break;
            default:
                break;
        }
        return productSwitchInfo;
    }

    private Long getTsIdByUidAndOrgId(String orgId, String uid, String tsType){
        Long psId = null;
        if (StringUtils.isBlank(uid)) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.UID_NULL);
        }
        if (StringUtils.isBlank(orgId)) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.ORG_ID_NULL);
        }
        //根据uid和orgId查到对应职位
        PostVo postVo = postService.listPostByUidAndOrgId(uid, orgId);
        log.info("查差标产线开关：用户职级postId={}, uid={}, orgId={}, tsType={}",postVo==null?"null":postVo.getId(), uid, orgId, tsType);
        //判断岗位是否存在
        if (postVo == null) {
            return null;
        }
        psId = postVo.getId();

        //根据职位查找对应机票差标
        TravelStandardPostVo vo = new TravelStandardPostVo();
        vo.setPostid(psId);
        vo.setTstype(tsType);
        List<TravelStandardPost> travelStandardPostVos = standPostService.list(vo);
        log.info("查差标产线开关:postId={}, uid={}, orgId={}, tsType={}, travelStandardPostVos={}",psId, uid, orgId, tsType,JsonUtils.toJsonString(travelStandardPostVos));
        if (travelStandardPostVos == null || travelStandardPostVos.size() == 0) {
            return null;
        }
        return travelStandardPostVos.get(0).getTsid();
    }

}
